package service;

import dao.UserDao;
import model.Page;
import model.User;

import java.sql.SQLException;
import java.util.List;

public class UserService {
    UserDao ud = new UserDao();

    public boolean register(User user) throws SQLException {
        if (this.isUsernameExist(user.getUsername())) {
            return false;
        } else if (this.isEmailExist(user.getEmail())) {
            return false;
        } else {
            ud.addUser(user);
            return true;
        }

    }

    public User login(String ue, String password) {
        User user = null;
        try {
            user = ud.selectByUsernamePassword(ue, password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if (user != null) {
            return user;
        } else {
            try {
                user = ud.selectByEmailPassword(ue, password);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            return user;
        }
    }

    public boolean updatePwd(User user) {
        user.setPassword(user.getPassword().trim());
        try {
            return ud.updatePwd(user);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return false;
    }

    public boolean updateUserAddress(User user) {
        try {
            return ud.updateUserAddress(user);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }

    public Page selectAllUser(int pageNumber) throws SQLException {
        Page page = new Page();
        //初始化默认一页多少数量
        int pageSize = 8;
        //设置页码
        page.setPageNumber(pageNumber);
        //对页码进行处理 好给Dao操作
        pageNumber = (pageNumber - 1) * pageSize;
        //总数量
        int totalcount;

        //有多少商品
        totalcount = ud.getUserCount();

        //一共好多页
        int totalPage = totalcount >= pageSize ? (int) (Math.ceil((double) totalcount / pageSize)) : 1;

        //防止用户输入的页码大于总页码
        if (page.getPageNumber() > totalPage) {
            page.setPageNumber(totalPage);
            pageNumber = (totalPage - 1) * pageSize;
        }

        //设置一页数量和总数量
        page.SetPageSizeAndTotalCount(pageSize, totalcount);
        //设置总页数
        page.setTotalPage(totalPage);

        List userList = ud.selectAllUser(pageNumber, pageSize);

        page.setList(userList);
        return page;
    }

    /**
     * 是否已存在用户名
     *
     * @param username 用户名
     * @return boolean 有true 没false
     */
    public boolean isUsernameExist(String username) {
        try {
            return ud.isUsernameExist(username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 是否已存在邮箱
     *
     * @param email 邮箱
     * @return boolean 有true 没false
     */
    public boolean isEmailExist(String email) {
        boolean flag = true;
        try {
            flag = ud.isEmailExist(email);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 获取用户信息通过id
     *
     * @param id 用户id
     * @return User || null
     */
    public User getUserById(int id) {
        try {
            return ud.getUserById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除用户通过id
     *
     * @param id 用户id
     * @return boolean
     */
    public boolean deleteUser(int id) {
        try {
            return ud.deleteUser(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
